package com.ljx.finance.biz.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.ljx.finance.biz.constant.RedisKeyConstant;
import com.ljx.finance.biz.dto.form.GetBase64CodeForm;
import com.ljx.finance.biz.service.MemberLoginService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
@RequiredArgsConstructor
public class MemberLoginServiceImpl implements MemberLoginService {

    final RedisTemplate<String,String> redisTemplate;
    /**
     * 获取客户端id
     * @return
     */
    @Override
    public String getClientId() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 获取验证码
     * @param form
     * @return
     */
    @Override
    public String getBase64Code(GetBase64CodeForm form) {

        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(300, 192, 5, 100);
        String code = lineCaptcha.getCode();
        //todo 将code 保存到redis中
        redisTemplate.opsForValue().set(RedisKeyConstant.GRAPHIC_VERIFICATION_CODE+form.getClientId(),
                code,
                15, TimeUnit.MINUTES);

        //返回图片
        return lineCaptcha.getImageBase64();
    }
}
